package com.gkwave.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class EmailMessageBuilder {
	
 public static void main(String args[]) throws Exception {
		/*FileReader fr = new FileReader("./src/email.html.template");
		BufferedReader br = new BufferedReader(fr);
		StringBuffer sBuffer = new StringBuffer();
		String s;
		while ((s = br.readLine()) != null) {
			if(s.contains("DATE")){
				s = s.replaceAll("DATE", Calendar.getInstance().toString());
				System.out.println(s);
			}
			sBuffer.append(s);
		}
		
		fr.close();*/
	 System.out.println(new EmailMessageBuilder("./src/email.html.template").getMessageContent(null, null, null));
	}

	private String fileLocation;
	private FileReader fileReader;
	private BufferedReader bufferReader;
	public EmailMessageBuilder(String fileLocation) {
		this.fileLocation = fileLocation;
		init();
		// TODO Auto-generated constructor stub
	}

	private void init() {
		// TODO Auto-generated method stub
		try {
			fileReader = new FileReader(fileLocation);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		bufferReader = new BufferedReader(fileReader);
	}
	
	public StringBuffer getMessageContent(String messageTitle, String messageType, String messageData){
		
		StringBuffer sBuffer = new StringBuffer();
		String s;
		
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd");
		String dateNow = formatter.format(currentDate.getTime());
		System.out.println("Now the date is :=>  " + dateNow);
		  
		try {
			while ((s = bufferReader.readLine()) != null) {
				if(s.contains("DATE")){
					s = s.replaceAll("DATE", dateNow);
				}
				if(s.contains("NOTIFICATION_TITLE")){
					s = s.replaceAll("NOTIFICATION_TITLE", messageTitle);
				}
				if(s.contains("NOTIFICATION_TYPE")){
					s = s.replaceAll("NOTIFICATION_TYPE", messageType);
				}
				if(s.contains("NOTIFICATION_DATA")){
					s = s.replaceAll("NOTIFICATION_DATA", messageData);
				}
				sBuffer.append(s);
			}
			fileReader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sBuffer;
	}
}